package players;

import java.util.ArrayList;
import java.util.Random;

import staticClasses.Action;
import staticClasses.Global;

public class GoalBehavior extends Behavior {
	/**
	 * The player that instantiates the classes, and so, the one being watched
	 */
	Player player;
	int hasJustJumped = 0;
	int hasJustPassed = 0;
	
	/**
	 * Constructor for a player with an easy behavior
	 * @param p the player being watched
	 */
	public GoalBehavior(Player p){
		this.player = p;
	}

	@Override
	public void execAction() {
		ArrayList<int[]> my_goal_line = Global.leftLine;
		if(!player.isInLocalTeam){
			my_goal_line = Global.rightLine;
		}
		
		if(Global.getBallOwner()!=null){player.ballState=0;}
		
		if(hasJustJumped>15){hasJustJumped=0;}
		else if(hasJustJumped>=1){hasJustJumped++;}
		
		if(hasJustPassed>5){hasJustPassed=0;}
		else if(hasJustPassed>=1){hasJustPassed++;}
		
		int distToBall = Global.bresenham(player.getPosition()[0], player.getPosition()[1],
				Global.ball.getPosition()[0], Global.ball.getPosition()[1]).size();
		
		int diffPos = Global.nearestOpponent(player).getPosition()[0]-Global.nearestTeamMate(player).getPosition()[0];
		boolean opponentTooClose = player.isInLocalTeam && diffPos<(50);
		opponentTooClose = opponentTooClose || (!player.isInLocalTeam && diffPos>(-50));
		
		if(opponentTooClose && Global.getBallOwner()!=null && Global.getBallOwner().equals(player)){}
		else if(hasJustJumped!=0){}
		else if(hasJustPassed!=0){}
		else if(Global.getBallOwner()!=null && distToBall<10 && !Global.getBallOwner().equals(player)){
			Global.setBallOwner(player);
			Global.ball.velocity=0;
			player.ballState=0;
		}
		else if((Global.getBallOwner()==null || Global.getBallOwner().isInLocalTeam!=player.isInLocalTeam)
				&& distToBall<20 && player.ballState!=1){
			//cas qqun vient de shooter
			double catchRate = ((double)player.intercept_level)/127.0;//from 0 to 200% chance to catch the ball
			catchRate *= 40.0/200.0;//from 0 to 40% chance to catch the ball
			catchRate += 0.6;//from 60 to 100% chance to catch the ball
			double X = (double)Global.ball.velocity;
			double chanceToMiss = 1.0-(180000.0/(X*X+100*X+180000.0));
			catchRate -= chanceToMiss;
			catchRate *= 100.0;//put it in percent
			Random r = new Random();
			int i=r.nextInt(101);
			if(i<=Math.round(catchRate)){
				Global.setBallOwner(player);
				Global.ball.velocity=0;
				player.ballState=0;
			}
			System.out.println(player.isInLocalTeam+" - score:"+i+"/"+Math.round(catchRate));
			hasJustJumped=1;
		}
		else if(distToBall<80 && Global.getBallOwner()!=null && !Global.getBallOwner().equals(player)){
			int[] pos = player.getPosition();
			ArrayList<int[]> path = Global.bresenham(pos[0], pos[1], Global.ball.getPosition()[0], Global.ball.getPosition()[1]);
			if(path.size()>10){
				double x_mov = (double)player.speed;
				x_mov = x_mov/127.0+1;//ratio speed of the player from 1 (with 0 speed) to 2 (with 255 speed)
				x_mov *= 4.0;//base speed is 4m/s
				x_mov /= 20;//speed per tick, a tick is 50ms
				x_mov*=10;//multiply by 10 cause there are 10 case per meter
				player.setPosition(path.get((int)Math.round(x_mov))[0], path.get((int)Math.round(x_mov))[1]);
			}
		}
		else if(Global.getBallOwner()!=null && Global.getBallOwner().equals(player)){
			player.ballState=1;
			Global.setBallOwner(null);
			Player teammate = Global.nearestTeamMate(player);
			teammate.ballState=2;
			Global.ball.targeted_case[0] = teammate.getPosition()[0];
			Global.ball.targeted_case[1] = teammate.getPosition()[1];
			Global.ball.velocity = (player.power+(((new Random()).nextInt(100)-50)*player.power)/100)/2;
			Global.ball.traveledDist = 0;
			hasJustPassed=1;
		}
		else if(Global.getBallOwner()==null && player.ballState!=1){
			if(Global.inArray(Global.ball.targeted_case[0], Global.ball.targeted_case[1], my_goal_line)){
				int[] pos = player.getPosition();
				ArrayList<int[]> path = Global.shortestLine(pos[0], pos[1], Global.ball.path);
				double x_mov = (double)player.speed;
				x_mov = x_mov/127.0+1;//ratio speed of the player from 1 (with 0 speed) to 2 (with 255 speed)
				x_mov *= 4.0;//base speed is 4m/s
				x_mov /= 20;//speed per tick, a tick is 50ms
				x_mov*=10;//multiply by 10 cause there are 10 case per meter
				if((int)Math.round(x_mov)<path.size()){
					player.setPosition(path.get((int)Math.round(x_mov))[0], path.get((int)Math.round(x_mov))[1]);
				}
				else{
					player.setPosition(path.get(path.size()-1)[0], path.get(path.size()-1)[1]);
				}
			}
		}
		else if(distToBall<100 && Global.getBallOwner()==null){
			int[] pos = player.getPosition();
			ArrayList<int[]> path = Global.bresenham(pos[0], pos[1], Global.ball.getPosition()[0], Global.ball.getPosition()[1]);
			if(path.size()>10){
				double x_mov = (double)player.speed;
				x_mov = x_mov/127.0+1;//ratio speed of the player from 1 (with 0 speed) to 2 (with 255 speed)
				x_mov *= 4.0;//base speed is 4m/s
				x_mov /= 20;//speed per tick, a tick is 50ms
				x_mov*=10;//multiply by 10 cause there are 10 case per meter
				player.setPosition(path.get((int)Math.round(x_mov))[0], path.get((int)Math.round(x_mov))[1]);
			}
		}
		else{
			int[] pos = player.getPosition();
			ArrayList<int[]> path = Global.bresenham(pos[0], pos[1], player.initialSpot.x, player.initialSpot.y);
			double x_mov = (double)player.speed;
			x_mov = x_mov/127.0+1;//ratio speed of the player from 1 (with 0 speed) to 2 (with 255 speed)
			x_mov *= 4.0;//base speed is 4m/s
			x_mov /= 20;//speed per tick, a tick is 50ms
			x_mov*=10;//multiply by 10 cause there are 10 case per meter
			if((int)Math.round(x_mov)<path.size()){
				player.setPosition(path.get((int)Math.round(x_mov))[0], path.get((int)Math.round(x_mov))[1]);
			}
			else{
				player.setPosition(path.get(path.size()-1)[0], path.get(path.size()-1)[1]);
			}
		}
	}

	@Override
	public Action whatToDo() {
		return null;
	}

}
